﻿using System.Collections.Generic;

namespace DBBase
{
    public class BaseQuery<T> where T : class
    {
        private string tableName;
        private string addString1;
        private string addString2;
        private string updateString;
        /// <summary>
        /// 初始化匿名类的表名
        /// </summary>
        public BaseQuery()
        {
            tableName = "t_" + typeof(T).Name;
            foreach (var item in typeof(T).GetProperties())
            {
                if (item.Name != "update_time")
                {
                    addString1 += item.Name + ",";
                    addString2 += "@" + item.Name + ",";
                    updateString += item.Name + "=@" + item.Name + ",";
                }
            }
            if (!string.IsNullOrEmpty(addString1)) addString1 = addString1.Substring(0, addString1.Length - 1);
            if (!string.IsNullOrEmpty(addString2)) addString2 = addString2.Substring(0, addString2.Length - 1);
            if (!string.IsNullOrEmpty(updateString)) updateString = updateString.Substring(0, updateString.Length - 1);

        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual int add(T model)
        {
            var sql = "insert into " + tableName + " (" + addString1 + ") values(" + addString2 + ");select @@IDENTITY";
            using (var db = new DBConnection())
            {
                return db.QueryModel<int>(sql, model);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual int update(T model)
        {
            var sql = "update " + tableName + " set " + updateString + " where id=@id";
            using (var db = new DBConnection())
            {
                return db.Execute(sql, model);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual int delete(int id)
        {
            var sql = "delete from " + tableName + " where id=@id";
            using (var db = new DBConnection())
            {
                return db.Execute(sql, new { id = id });
            }
        }
        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual T get(int id)
        {
            var sql = "select * from " + tableName + " where id=@id";
            using (var db = new DBConnection())
            {
                return db.QueryModel<T>(sql, new { id = id });
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="pager"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagecount"></param>
        /// <returns></returns>
        public virtual List<T> getlist(string strWhere, out int pager, int pageindex = 1, int pagecount = 20)
        {
            if (!string.IsNullOrEmpty(strWhere))
            {
                strWhere = " where " + strWhere;
            }
            var sqlcount = "select count(id) from " + tableName + strWhere;
            var sql = "select * from " + tableName + strWhere + "  order by id desc limit " + (pageindex - 1) * pagecount + "," + pagecount;
            using (var db = new DBConnection())
            {
                int count = db.QueryModel<int>(sqlcount);
                int a = count / pagecount;
                if (count % pagecount > 0) a++;
                pager = a;
                return db.QueryList<T>(sql);
            }
        }
        /// <summary>
        /// 获取分页结果
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagecount"></param>
        /// <returns></returns>
        public virtual Models.PageBase<T> getlist(string strWhere, int pageindex = 1, int pagecount = 20)
        {
            if (!string.IsNullOrEmpty(strWhere))
            {
                strWhere = " where " + strWhere;
            }
            var sqlcount = "select count(id) from " + tableName + strWhere;
            var sql = "select * from " + tableName + strWhere + "  order by id desc limit " + (pageindex - 1) * pagecount + "," + pagecount;
            using (var db = new DBConnection())
            {
                var result = new Models.PageBase<T>();
                int count = db.QueryModel<int>(sqlcount);
                int a = count / pagecount;
                if (count % pagecount > 0) a++;
                result.pager = a;
                result.list = db.QueryList<T>(sql);
                return result;
            }
        }
    }
}
